package com.globant.dashboard.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.globant.dashboard.model.Language;
import com.globant.dashboard.model.User;
import com.globant.dashboard.persist.UserDAO;
import com.globant.dashboard.persist.UserValidator;

public class UserServiceImp implements UserService{

	@Autowired
	private UserValidator userValidator; 
	
	@Autowired
	private UserDAO userDAO;
	
	@Autowired
	@Qualifier("spanish")
	private Language spanish;
	@Autowired
	@Qualifier("english")
	private Language english;
	@Autowired
	@Qualifier("french")
	private Language french;
	@Autowired
	@Qualifier("german")
	private Language german;
	@Autowired
	@Qualifier("italian")
	private Language italian;
	
	public List<User> getAllUsers(int pageNumber, int pageSize){
		return userDAO.getAll(pageNumber,pageSize);
	}
	
	public List<User> getByCriteria(String info, int pageNumber, int pageSize){
		return userDAO.getByCriteria(info,pageNumber,pageSize);
	}
	
	public void insertUser(User user) throws RuntimeException{
		setLanguages(user);
		if(userValidator.isNewUser(user.getEmail())){
			if(userValidator.emailValidator(user)){
				if(userValidator.nameValidator(user)){
					userDAO.insert(user);
				}else{
					throw new RuntimeException("Incorrect name.");
				}
			}else{
				throw new RuntimeException("Incorrect e-mail.");
			}
		}else{
			throw new RuntimeException("User already registred.");
		}
		
	}
	
	public void deleteUser(User user){
		userDAO.delete(user);
	}
	
	public void updateUser(User user) throws RuntimeException{
		setLanguages(user);
		if(userValidator.emailValidator(user)){
			if(userValidator.nameValidator(user)){
				User dbUser = userDAO.getByEmail(user.getEmail());
				if(dbUser == null){
					userDAO.update(user);
				} else {
					if(dbUser.getId() == user.getId()){
						userDAO.update(user);
					} else {
						throw new RuntimeException("Email is already registred.");
					}
				}
			}else{
				throw new RuntimeException("Invalid name.");
			}
		}else{
			throw new RuntimeException("Invalid email.");
		}
	}
	
	public void setLanguages(User user) {
		List<Language> newList= new ArrayList<Language>();
		List<Language> userLanguages=user.getLanguages();
		if(userLanguages==null) return;
		for (Language l:userLanguages){
			if(l.getLanguage().equals("Spanish"))
				newList.add(spanish);
			if(l.getLanguage().equals("English"))
				newList.add(english);
			if(l.getLanguage().equals("German"))
				newList.add(german);
			if(l.getLanguage().equals("French"))
				newList.add(french);
			if(l.getLanguage().equals("Italian"))
				newList.add(italian);
		}
		user.setLanguages(newList);
	}

	public User getUserById(int id){
		return userDAO.getById(id);
	}
	
	public User getUserByEmail(String email){
		return userDAO.getByEmail(email);
	}
	
	public int countUsers(){
		return userDAO.countUsers();
	}

	public int countUsersByCriteria(String criteria) {
		return userDAO.countUserByCriteria(criteria);
	}

}